package com.clouddot.baipai.model;

import android.util.Log;

import com.clouddot.baipai.infrastructure.environment.Env;
import com.clouddot.baipai.infrastructure.environment.Environment;
import com.clouddot.baipai.model.beam.TargetUrl;
import com.clouddot.baipai.model.beam.WeiChatNews.WeiChatNewsResponse;
import com.clouddot.baipai.model.beam.applyLeave.applyLeaveResponse;
import com.clouddot.baipai.model.beam.bindBanPai.bindBanPaiResponse;
import com.clouddot.baipai.model.beam.getAllClass.allclassInfo;
import com.clouddot.baipai.model.beam.getAllClass.getAllClassResponse;
import com.clouddot.baipai.model.beam.getArcticById.getArcticByIdResponse;
import com.clouddot.baipai.model.beam.getArcticList.ariticInfo;
import com.clouddot.baipai.model.beam.getArcticList.getArcticListResponse;
import com.clouddot.baipai.model.beam.getClassHonorById.getClassHonorByIdResponse;
import com.clouddot.baipai.model.beam.getClassHonorById.honorInfo;
import com.clouddot.baipai.model.beam.getClassInfo.classInfo;
import com.clouddot.baipai.model.beam.getClassInfo.getClassInfoResponse;
import com.clouddot.baipai.model.beam.getClassInfoById.getClassInfoByIdResponse;
import com.clouddot.baipai.model.beam.getClassInfoList.classInfoItem;
import com.clouddot.baipai.model.beam.getClassInfoList.getClassInfoListResponse;
import com.clouddot.baipai.model.beam.getClassMemberById.getClassMemberByIdResponse;
import com.clouddot.baipai.model.beam.getClassMemberById.studentInfo;
import com.clouddot.baipai.model.beam.getClassType.getClassTypeResponse;
import com.clouddot.baipai.model.beam.getCourseById.courseInfoByid;
import com.clouddot.baipai.model.beam.getCourseById.getCourseByIdResponse;
import com.clouddot.baipai.model.beam.getCourseList.courseInfo;
import com.clouddot.baipai.model.beam.getCourseList.getCourseListResponse;
import com.clouddot.baipai.model.beam.getCourseTable.classTime;
import com.clouddot.baipai.model.beam.getCourseTable.dayInfo;
import com.clouddot.baipai.model.beam.getCourseTable.getCourseTableResponse;
import com.clouddot.baipai.model.beam.getCourseTable.weeksName;
import com.clouddot.baipai.model.beam.getCurrentCourse.getCurrentCourseResponse;
import com.clouddot.baipai.model.beam.getLeaveRecode.LeaveInfo;
import com.clouddot.baipai.model.beam.getLeaveRecode.getLeaveRecordResponse;
import com.clouddot.baipai.model.beam.getLeaveType.LeaveTypeInfo;
import com.clouddot.baipai.model.beam.getLeaveType.getLeaveTypeResponse;
import com.clouddot.baipai.model.beam.getPersonInfo.getPersonInfoResponse;
import com.clouddot.baipai.model.beam.getRecord.attentionRecord;
import com.clouddot.baipai.model.beam.getRecord.classAttendRecord;
import com.clouddot.baipai.model.beam.getRecord.getRecordByIdResponse;
import com.clouddot.baipai.model.beam.getRecord.getRecordResponse;
import com.clouddot.baipai.model.beam.getSchoolInfo.ArticleData;
import com.clouddot.baipai.model.beam.getSchoolInfo.BaseData;
import com.clouddot.baipai.model.beam.getSchoolInfo.ThumbnailData;
import com.clouddot.baipai.model.beam.getSchoolInfo.getSchoolInfoResponse;
import com.clouddot.baipai.model.beam.login.loginResponse;
import com.clouddot.baipai.model.beam.postPhoto.postPhotoResponse;
import com.clouddot.baipai.ui.util.AnalyzeRequestTypeUtil;
import com.clouddot.baipai.ui.util.ListDataSaveUtil;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by wxy on 2018/4/9.
 */

public class TasksRepository implements TaskDataSource {

    private Gson gson;

    public TasksRepository() {
        this.gson = new Gson();
    }

    @Override
    public String getWeatherInfo(String name) {
        return Env.getNetConnect().getWeather(Env.getTargetUrl().getWeatherUrl + name);
    }

    @Override
    public getSchoolInfoResponse getSchoolInfo(String tag) {
        String url = Env.getTargetUrl().getSchoolInfoUrl + tag;
        String flag = "获取学校信息 :";
        String result = Env.getNetConnect().getConnect(flag ,url);
        getSchoolInfoResponse response = new getSchoolInfoResponse();
        try {
            if (!result.isEmpty()) {
                if (!result.equals("error")) {
                    Map<String, Object> map = ListDataSaveUtil.getDataDifListFromNet(result, AnalyzeRequestTypeUtil.getSchoolInfoType(), AnalyzeRequestTypeUtil.getSchoolInfoName());
                    response.BaseData = (BaseData) map.get("BaseData");
                    response.ArticleData = (ArticleData) map.get("ArticleData");
                    if ((List<ThumbnailData>) map.get("ThumbnailData") != null) {
                        response.ThumbnailDatas = (List<ThumbnailData>) map.get("ThumbnailData");
                        response.url = Env.getAppContext().IP + response.ThumbnailDatas.get(0).getRealPathName();
                    }
                    JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                    if(jsonObject.has("VideoData")) {
                        JsonArray array =  jsonObject.getAsJsonArray("VideoData");
                        for(JsonElement element :array){
                            response.VideoData.add((getSchoolInfoResponse.VideoData) gson.fromJson(element, new TypeToken<getSchoolInfoResponse.VideoData>() {
                            }.getType()));
//                            JsonObject jsonObject1 = new JsonParser().parse(element.toString()).getAsJsonObject();
//                            if(jsonObject1.has("Attachment")){
//                                JsonObject jsonObject2 = jsonObject1.getAsJsonObject("Attachment");
//                                response.imageurl = jsonObject2.get("RealPathName").getAsString();
//                            }else{
//                                response.imageurl = "";
//                            }
                        }
                    }
                    response.setSuccess(true);
                } else {
                    response.setErrMsg("网络错误，请检查网络");
                    Env.info(response.getErrMsg());
                }
            } else {
                response.setErrMsg(tag + "获取失败");
                Env.info(response.getErrMsg());

            }
        }
        catch (Exception e) {
            response.setErrMsg("访问失败了");
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
        }finally {
            return response;

        }
    }

    @Override
    public getClassInfoResponse getClassInfo(String tag) {
        String url = Env.getTargetUrl().getClassInfoUrl + tag;
        String flag = "获取班级信息 :";

        String result = Env.getNetConnect().getConnect(flag,url);
        getClassInfoResponse response = new getClassInfoResponse();
        try {
            if (!result.isEmpty()) {
                if (!result.equals("error")) {
                    JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                    if (jsonObject.entrySet().size() > 1) {
                        response.ClassId = jsonObject.get("ClassId").getAsString();
                        response.ClassName = jsonObject.get("ClassName").getAsString();
                        response.SemesterId = jsonObject.get("SemesterId").getAsString();
                        JsonElement element = jsonObject.get("Semester");
                        JsonObject object = new JsonParser().parse(element.toString()).getAsJsonObject();
                        response.current = gson.fromJson(object.get("Current"), new TypeToken<classInfo>() {
                        }.getType());
                        response.Choosing = gson.fromJson(object.get("Choosing"), new TypeToken<classInfo>() {
                        }.getType());
                        Env.getAppContext().classId = response.ClassId;
                        Env.getAppContext().className = response.ClassName;
                        response.setSuccess(true);
                    } else {
                        response.setErrMsg("班级获取失败，请点击重试");
                        Env.info(response.getErrMsg());
                    }
                } else {
                    response.setErrMsg("网络错误，请检查网络");
                    Env.info(response.getErrMsg());
                }
            }
        }catch (Exception e) {
            response.setErrMsg("访问失败了");
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
        }finally {
            return response;

        }
    }

    @Override
    public getPersonInfoResponse getPersonInfo() {
        String url  = Env.getTargetUrl().getPersonInfoUrl;
        String flag = "获取个人信息 :";

        String result = Env.getNetConnect().getConnect(flag,url);
        getPersonInfoResponse response = new getPersonInfoResponse();
        try {
            if (!result.isEmpty()) {
                if (!result.equals("error")) {
                    JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                    if (jsonObject.entrySet().size() > 1) {
                        response.Role = jsonObject.get("Role").getAsString();
                        response.RealName = jsonObject.get("RealName").getAsString();
                        response.Email = jsonObject.get("Email").getAsString();
                        response.Sex = jsonObject.get("Sex").getAsString();
                        response.Mobile = jsonObject.get("Mobile").getAsString();
                        response.ClassName = jsonObject.get("ClassName").getAsString();
                        response.Photo = jsonObject.get("Photo").getAsString();
                        response.StudentId = jsonObject.get("StudentId").getAsString();
                        Env.getAppContext().StudentId = response.StudentId;
                        Env.info("StudentId" + Env.getAppContext().StudentId);
                        response.setSuccess(true);
                    }
                } else {
                    response.setErrMsg("网络错误，请检查网络");
                    Env.info(response.getErrMsg());
                }
            } else {
                response.setErrMsg("个人信息获取失败");
                Env.info(response.getErrMsg());
            }
        }catch (Exception e) {
            response.setErrMsg("访问失败了");
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
        }finally {
            return response;

        }
    }

    @Override
    public loginResponse login(String code, String imgUrl) {
        String url = Env.getTargetUrl().loginUrl + code+"&filepath="+imgUrl;
        String flag = "登录操作 :";

        String result = Env.getNetConnect().getConnect(flag,url);
        loginResponse response = new loginResponse();
        try {
            if (!result.isEmpty()) {
                if (!result.equals("error")) {
                    JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                    response.res = jsonObject.get("res").getAsString();
                    if (!response.res.equals("fail")) {
                        response.setSuccess(true);
                        getPersonInfo();
                    } else {
                        response.setErrMsg("这张卡不属于这个班级，请重新刷卡");
                        Env.info(response.getErrMsg());
                    }
                } else {
                    response.setErrMsg("网络错误，请检查网络");
                    Env.info(response.getErrMsg());
                }
            }
        }catch (Exception e) {
            response.setErrMsg("访问失败了");
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
        }finally {
            return response;

        }
    }
    @Override
    public loginResponse login(String code) {
        String url =Env.getTargetUrl().loginUrl + code;
        String flag = "登录操作 :";

        String result = Env.getNetConnect().getConnect(flag,url);
        loginResponse response = new loginResponse();
        try {

            if (!result.isEmpty()) {
                if (!result.equals("error")) {
                    JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                    response.res = jsonObject.get("res").getAsString();
                    if (!response.res.equals("fail")) {
                        response.setSuccess(true);
                        getPersonInfo();
                    } else {
                        response.setErrMsg("这张卡不属于这个班级，请重新刷卡");
                        Env.info(response.getErrMsg());
                    }
                } else {
                    response.setErrMsg("网络错误，请检查网络");
                    Env.info(response.getErrMsg());
                }
            }
        }
        catch (Exception e) {
            response.setErrMsg("访问失败了");
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
        }finally {
            return response;

        }
    }

    @Override
    public getRecordResponse getRecord() {
        String url = Env.getTargetUrl().getRecordUrl+ Env.getAppContext().classId;
        String flag = "获取考勤记录 :";

        String result = Env.getNetConnect().getConnect(flag,url);
        getRecordResponse response = new getRecordResponse();
        try {
            if (!result.isEmpty()) {
                if (!result.equals("error")) {
                    response.list = ListDataSaveUtil.getDataListFromNet(result, classAttendRecord.class);
                    response.setSuccess(true);
                } else {
                    response.setErrMsg("网络错误，请检查网络");
                    Env.info(response.getErrMsg());
                }
            } else {
                response.setErrMsg("考勤获取失败");
                Env.info(response.getErrMsg());

            }
        }catch (Exception e) {
            response.setErrMsg("访问失败了");
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
        }finally {
            return response;

        }

    }

    @Override
    public getRecordByIdResponse getRecordBySid(int pageIndex, int pageSize) {
        String url =Env.getTargetUrl().getRecordByIdUrl+ TargetUrl.pageIndex+pageIndex+ TargetUrl.pageSize+pageSize;

        String flag = "获取考勤记录 :";

        String result = Env.getNetConnect().getConnect(flag,url);
        getRecordByIdResponse response = new getRecordByIdResponse();
        try {
            if (!result.isEmpty()) {
                if (!result.equals("error")) {
                    JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                    response.PageCount = jsonObject.get("PageCount").getAsString();
                    response.PageIndex = jsonObject.get("PageIndex").getAsString();
                    response.PageSize = jsonObject.get("PageSize").getAsString();
                    JsonArray array = jsonObject.get("EntityList").getAsJsonArray();
                    for (JsonElement element : array) {
                        response.list.add((attentionRecord) gson.fromJson(element, new TypeToken<attentionRecord>() {
                        }.getType()));
                    }
                    response.setSuccess(true);
                } else {
                    response.setErrMsg("网络错误，请检查网络");
                    Env.info(response.getErrMsg());
                }
            } else {
                response.setErrMsg("考勤获取失败");
                Env.info(response.getErrMsg());

            }
        }catch (Exception e) {
            response.setErrMsg("访问失败了");
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
        }finally {
            return response;

        }

    }

    @Override
    public getArcticByIdResponse getArcticById(String Id) {
        String url = Env.getTargetUrl().getAriticByIdUrl+ Id;
        String flag = "获取文章信息内容 :";

        String result = Env.getNetConnect().getConnect(flag,url);
        getArcticByIdResponse response = new getArcticByIdResponse();
        try {
            if (!result.isEmpty()) {
                if (!result.equals("error")) {

                    Map<String, Object> map = ListDataSaveUtil.getDataDifListFromNet(result, AnalyzeRequestTypeUtil.getSchoolInfoType(), AnalyzeRequestTypeUtil.getSchoolInfoName());
                    response.BaseData = (BaseData) map.get("BaseData");
                    response.ArticleData = (ArticleData) map.get("ArticleData");
                    response.ThumbnailDatas = (List<ThumbnailData>) map.get("ThumbnailData");
                    response.setSuccess(true);
                } else {
                    response.setErrMsg("网络错误，请检查网络");
                    Env.info(response.getErrMsg());
                }
            } else {
                response.setErrMsg("内容获取失败");
                Env.info(response.getErrMsg());

            }
        }catch (Exception e) {
            response.setErrMsg("访问失败了");
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
        }finally {
            return response;

        }

    }

    @Override
    public getArcticListResponse getArcticList(String type, int pageIndex, int pageSize) {
        String url = Env.getTargetUrl().getAriticListUrl + type + TargetUrl.pageIndex+pageIndex+ TargetUrl.pageSize+pageSize;
        String flag = "获取文章列表 :";

        String result = Env.getNetConnect().getConnect(flag,url);
        getArcticListResponse response = new getArcticListResponse();
        try {
            if (!result.isEmpty()) {
                if (!result.equals("error")) {
                    JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                    response.PageCount = jsonObject.get("PageCount").getAsString();
                    response.PageIndex = jsonObject.get("PageIndex").getAsString();
                    response.PageSize = jsonObject.get("PageSize").getAsString();
                    JsonArray array = jsonObject.get("EntityList").getAsJsonArray();
                    for (JsonElement element : array) {
                        response.list.add((ariticInfo) gson.fromJson(element, new TypeToken<ariticInfo>() {
                        }.getType()));
                    }
                    response.setSuccess(true);
                } else {
                    response.setErrMsg("网络错误，请检查网络");
                    Env.info(response.getErrMsg());
                }
            } else {

                response.setErrMsg("内容为空");
                Env.info(response.getErrMsg());


            }
        }catch (Exception e) {
            response.setErrMsg("访问失败了");
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
        }finally {
            return response;

        }
    }

    @Override
    public getClassInfoListResponse getClassInfoList(String type, int pageIndex, int pageSize) {
        String url = Env.getTargetUrl().getClassInfoListUrl  + type+ TargetUrl.classId+ Env.getAppContext().classId+ TargetUrl.pageIndex+pageIndex+ TargetUrl.pageSize+pageSize;
        String flag = "获取班级通知信息列表 :";
        String result = Env.getNetConnect().getConnect(flag,url);
        getClassInfoListResponse response = new getClassInfoListResponse();
        try {
            if (!result.isEmpty()) {
                if (!result.equals("error")) {
                    JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                    response.PageCount = jsonObject.get("PageCount").getAsString();
                    response.PageIndex = jsonObject.get("PageIndex").getAsString();
                    response.PageSize = jsonObject.get("PageSize").getAsString();
                    JsonArray array = jsonObject.get("EntityList").getAsJsonArray();
                    for (int i = 0; i < array.size(); i++) {
                        response.list.add((classInfoItem) gson.fromJson(array.get(i), new TypeToken<classInfoItem>() {
                        }.getType()));
                        JsonObject jsonObject1 = new JsonParser().parse(array.get(i).toString()).getAsJsonObject();
                        JsonArray jsonArray = jsonObject1.getAsJsonArray("Attachments");
                        int size = jsonArray.size() < 5 ? jsonArray.size() : 5;
                         for(int j = 0 ; j < size ;j++) {
                            response.list.get(i).getStrings().add(new JsonParser().parse(jsonArray.get(j).toString()).getAsJsonObject().get("RealPathName").getAsString());
                        }

                    }
                    response.setSuccess(true);
                } else {
                    response.setErrMsg("网络错误，请检查网络");
                    Env.info(response.getErrMsg());
                }

            } else {
                response.setErrMsg("班级信息获取失败");
                Env.info(response.getErrMsg());

            }
        }
        catch (Exception e) {
            response.setErrMsg("访问失败了");
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
        }finally {
            return response;

        }
    }

    @Override
    public getClassInfoByIdResponse getClassInfoById(String Id) {
        String url = Env.getTargetUrl().getGetClassInfoByIdUrl  + Id;
        String flag = "获取班级通知信息内容 :";

        String result = Env.getNetConnect().getConnect(flag,url);
        getClassInfoByIdResponse response = new getClassInfoByIdResponse();
        try {
            if (!result.isEmpty()) {
                if (!result.equals("error")) {
                    JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                    response.Id = jsonObject.get("Id").getAsString();
                    response.UserId = jsonObject.get("UserId").getAsString();
                    response.Title = jsonObject.get("Title").getAsString();
                    response.CreateTime = jsonObject.get("CreateTime").getAsString();
                    response.Detail = jsonObject.get("Detail").getAsString();
                    response.setSuccess(true);
                } else {
                    response.setErrMsg("网络错误，请检查网络");
                    Env.info(response.getErrMsg());
                }
            } else {
                response.setErrMsg("班级通知获取失败");
                Env.info(response.getErrMsg());


            }
        }
        catch (Exception e) {
            response.setErrMsg("访问失败了");
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
        }finally {
            return response;

        }
    }


    @Override
    public getCourseByIdResponse getCourseById(String Id, int pageIndex , int pageSize) {
        String url = Env.getTargetUrl().getCourseInfoByIdUrl +Id + TargetUrl.pageIndex + pageIndex+ TargetUrl.pageSize+pageSize;
        String flag = "获取作业内容 :";
        String result = Env.getNetConnect().getConnect(flag,url);
        getCourseByIdResponse response = new getCourseByIdResponse();
        try {
            if (!result.isEmpty()) {
                if (!result.equals("error")) {

                    JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                    response.PageCount = jsonObject.get("PageCount").getAsString();
                    response.PageIndex = jsonObject.get("PageIndex").getAsString();
                    response.PageSize = jsonObject.get("PageSize").getAsString();
                    JsonArray array = jsonObject.get("EntityList").getAsJsonArray();
                    for (JsonElement element : array) {
                        response.list.add((courseInfoByid) gson.fromJson(element, new TypeToken<courseInfoByid>() {
                        }.getType()));
                    }
                    response.setSuccess(true);
                } else {
                    response.setErrMsg("网络错误，请检查网络");
                    Env.info(response.getErrMsg());
                }
            } else {
                response.setErrMsg("作业内容获取失败");
                Env.info(response.getErrMsg());


            }
        }
        catch (Exception e) {
            response.setErrMsg("访问失败了");
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
        }finally {
            return response;

        }
    }

    @Override
    public getCourseListResponse getCourseList(String Id) {
        String url = Env.getTargetUrl().getCourseListUrl+ Id;
        String flag = "获取作业列表 :";
        getCourseListResponse response = new getCourseListResponse();
        String result = Env.getNetConnect().getConnect(flag,url);
        try {
            if (!result.isEmpty()) {
                if (!result.equals("error")) {

                    if (result.equals("[]")) {
                        response.setErrMsg("暂时无课程");
                    } else {
                        response.list = ListDataSaveUtil.getDataListFromNet(result, courseInfo.class);
                        response.setSuccess(true);
                    }
                } else {
                    response.setErrMsg("网络错误，请检查网络");
                    Env.info(response.getErrMsg());
                }
            } else {
                response.setErrMsg("课程获取失败");
                Env.info(response.getErrMsg());

            }
        }catch (Exception e) {
            response.setErrMsg("访问失败了");
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
        }finally {
            return response;

        }

    }

    @Override
    public getAllClassResponse getAllClassList() {
        String url = Env.getTargetUrl().getAllClassUrl;
        String flag = "获取所有班级的信息列表 :";

        String result = Env.getNetConnect().getConnect(flag,url);
        getAllClassResponse response = new getAllClassResponse();
        try{
            if(!result.isEmpty()){
                if(!result.equals("error")) {

                    response.list = ListDataSaveUtil.getDataListFromNet(result, allclassInfo.class);
                    response.setSuccess(true);
                }else{
                    response.setErrMsg("网络错误，请检查网络");
                    Env.info(response.getErrMsg());
                }
            }else{
                response.setErrMsg("全部班级获取失败");
                Env.info(response.getErrMsg());
            }
        }catch (Exception e) {
            response.setErrMsg("访问失败了");
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
        }finally {
            return response;

        }

    }

    @Override
    public getClassMemberByIdResponse getClassMemberByIdList(String classId, int pageIndex, int pageSize) {
        String url = Env.getTargetUrl().getClassMemberByIdUrl + classId+ TargetUrl.pageIndex + pageIndex + TargetUrl.pageSize + pageSize;
        String flag = "获取班级成员信息列表 :";
        String result = Env.getNetConnect().getConnect(flag,url);
        getClassMemberByIdResponse response = new getClassMemberByIdResponse();
        try{
            if(!result.isEmpty()){
            if(!result.equals("error")) {
                JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                response.PageCount = jsonObject.get("PageCount").getAsString();
                response.PageIndex = jsonObject.get("PageIndex").getAsString();
                response.PageSize = jsonObject.get("PageSize").getAsString();
                JsonArray array = jsonObject.get("EntityList").getAsJsonArray();
                for (JsonElement element : array) {
                    response.list.add((studentInfo) gson.fromJson(element, new TypeToken<studentInfo>() {
                    }.getType()));
                }
                response.setSuccess(true);
            }else{
                response.setErrMsg("网络错误，请检查网络");
                Env.info(response.getErrMsg());
            }
        }else{
            response.setErrMsg("班级成员获取失败");
            Env.info(response.getErrMsg());

        }
    }catch (Exception e) {
        response.setErrMsg("访问失败了");
        Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
    }finally {
            Env.info(result);
        return response;

    }
    }

    @Override
    public getClassHonorByIdResponse getClassHonorByIdList(String classId, int pageIndex, int pageSize) {
        String url = Env.getTargetUrl().getClassHonorByIdUrl + classId + TargetUrl.pageIndex + pageIndex + TargetUrl.pageSize + pageSize;
        String flag = "获取班级荣誉列表 :";
        String result = Env.getNetConnect().getConnect(flag,url);
        getClassHonorByIdResponse response = new getClassHonorByIdResponse();
        try{
            if(!result.isEmpty()){
                if(!result.equals("error")) {

                    JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                    response.PageCount = jsonObject.get("PageCount").getAsString();
                    response.PageIndex = jsonObject.get("PageIndex").getAsString();
                    response.PageSize = jsonObject.get("PageSize").getAsString();
                    JsonArray array = jsonObject.get("EntityList").getAsJsonArray();
                    for (JsonElement element : array) {
                        response.list.add((honorInfo) gson.fromJson(element, new TypeToken<honorInfo>() {
                        }.getType()));
                    }
                    response.setSuccess(true);
                }else{
                    response.setErrMsg("网络错误，请检查网络");
                    Env.info(response.getErrMsg());
                }
            }else{
                response.setErrMsg("班级荣誉获取失败");
                Env.info(response.getErrMsg());

            }
        }catch (Exception e) {
            response.setErrMsg("访问失败了");
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
        }finally {
            return response;

        }
    }

    @Override
    public bindBanPaiResponse bindBanPai(String classId, String cardDeviceId) {
        String url = Env.getTargetUrl().bindBanPaiUrl + classId + TargetUrl.cardDeviceId + cardDeviceId;
        String flag = "绑定班级到班牌设备中 :";

        String result = Env.getNetConnect().getConnect(flag,url);
        bindBanPaiResponse response = new bindBanPaiResponse();
        try{
            if (!result.isEmpty()){
                if(!result.equals("error")) {

                    JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                    response.res = jsonObject.get("res").getAsString();
                    if (response.res.equals("success")) {
                        response.setSuccess(true);
                    }
                }else{
                    response.setErrMsg("网络错误，请检查网络");
                    Env.info(response.getErrMsg());
                }
            }else {
                response.setErrMsg("绑定失败");
                Env.info(response.getErrMsg());
            }
        }catch (Exception e) {
            response.setErrMsg("访问失败了");
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
        }finally {
            return response;

        }
    }

    @Override
    public getCourseTableResponse getcourseTable(String ClassId) {
        String url = Env.getTargetUrl().getCourseTableUrl + ClassId;
        String flag = "获取班级课表 :";

        String result = Env.getNetConnect().getConnect2(flag,url);
        getCourseTableResponse response = new getCourseTableResponse();
        try{
            if (!result.isEmpty()){
                if(!result.equals("error")) {

                    if (result.equals("{}")) {
                        response.setErrMsg("暂时无课程安排");
                    } else {
                        JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                        //获取名字和时间
                        JsonElement TimetableSetting = jsonObject.getAsJsonObject("TimetableSetting");
                        JsonObject Parameters = new JsonParser().parse(TimetableSetting.toString()).getAsJsonObject();
                        String parameters = Parameters.get("Parameters").getAsString();
                        JsonObject Parameter = new JsonParser().parse(parameters).getAsJsonObject();
                        JsonArray WeeksArray = Parameter.get("Weeks").getAsJsonArray(); //获取周期
                        JsonArray ClassesArray = Parameter.get("Classes").getAsJsonArray();//获取上课时间

                        JsonElement TimeTable = jsonObject.getAsJsonObject("TimeTable");
                        JsonObject Data = new JsonParser().parse(TimeTable.toString()).getAsJsonObject();
                        JsonArray TableArray = Data.get("Data").getAsJsonArray(); //获取课程内容

                        if (!WeeksArray.isJsonNull() && !ClassesArray.isJsonNull() && !TableArray.isJsonNull()) {
                            for (JsonElement element : WeeksArray) {
                                response.weeksNames.add((weeksName) gson.fromJson(element, new TypeToken<weeksName>() {
                                }.getType()));
                            }
                            for (JsonElement element : ClassesArray) {
                                response.classTimes.add((classTime) gson.fromJson(element, new TypeToken<classTime>() {
                                }.getType()));
                            }
                            //获取课程内容
                            for (JsonElement element : TableArray) {
                                List<dayInfo> dayInfos = new ArrayList<>();
                                JsonArray DayInfoArray = element.getAsJsonArray();
                                for (JsonElement element1 : DayInfoArray) {
                                    JsonObject Item1 = new JsonParser().parse(element1.toString()).getAsJsonObject();
                                    JsonArray ItemArray1 = Item1.get("ItemList").getAsJsonArray();
                                    dayInfo dayInfo = new dayInfo();
                                    if (ItemArray1.size() > 0) {
                                        JsonElement element2 = ItemArray1.get(0);
                                        JsonObject Item2 = new JsonParser().parse(element2.toString()).getAsJsonObject();
                                        dayInfo.setCourseName(Item2.get("CourseName").getAsString());
                                        dayInfo.setMessage(Item2.get("Message").getAsString());
                                        dayInfo.setTeacherName(Item2.get("TeacherName").getAsString());
                                        dayInfo.setBeginWeek(Item2.get("BeginWeek").getAsString());
                                        dayInfo.setEndWeek(Item2.get("EndWeek").getAsString());
                                        dayInfo.setRemark(Item2.get("Remark").getAsString());
                                        dayInfo.setWeekHours(Item2.get("WeekHours").getAsString());
                                    } else {
                                        dayInfo.setCourseName("");
                                        dayInfo.setMessage("");
                                        dayInfo.setTeacherName("");
                                    }
                                    dayInfos.add(dayInfo);
                                }
                                response.dayInfos.add(dayInfos);
                            }
                            response.setSuccess(true);
                        } else {
                            response.setErrMsg("获取的课表数据不完整，请到后台上布完整的课表数据");
                        }
                    }
                }else{
                    response.setErrMsg("网络错误，请检查网络");
                    Env.info(response.getErrMsg());
                }
            }else {
                response.setErrMsg("获取课表失败,请刷卡");
                Env.info(response.getErrMsg());
            }
        }catch (Exception e) {
            response.setErrMsg("访问失败了");
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
        }finally {
            return response;

        }

    }

    @Override
    public getCurrentCourseResponse getCurrentCourse(String ClassId) {
        String url = Env.getTargetUrl().getCurrentCourseUrl + ClassId;
        String flag = "获取当前正在上课的课程 :";

        String result = Env.getNetConnect().getConnect(flag,url);
        getCurrentCourseResponse response = new getCurrentCourseResponse();
        try {
            if (!result.isEmpty()) {
                if (!result.equals("error")) {
                    JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                    if (!jsonObject.get("WeekNo").getAsString().equals("0")) {
                        if (jsonObject.has("TeachingClassName")) {
                            response.TeachingClassName = jsonObject.get("TeachingClassName").getAsString();
                        }else{
                            response.TeachingClassName = "";
                        }
                        if (jsonObject.has("TeacherName")){
                            response.TeacherName = jsonObject.get("TeacherName").getAsString();
                        }else{
                            response.TeacherName = "";
                        }

                        if (jsonObject.has("CourseName")) {

                            response.CourseName = jsonObject.get("CourseName").getAsString();
                        }else{
                            response.CourseName = "";
                        }

                        response.ClassNo = jsonObject.get("ClassNo").getAsString();
                        response.WeekNo = jsonObject.get("WeekNo").getAsString();
                        response.setSuccess(true);
                        Env.info(response.ClassNo);
                    } else {
                        response.setErrMsg("当前时间段无上课课程");
                        Env.info(response.getErrMsg());
                    }
                } else {
                    response.setErrMsg("网络错误，请检查网络");
                    Env.info(response.getErrMsg());
                }
            }
        }catch (Exception e) {
            response.setErrMsg("访问失败了");
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
        }finally {
            return response;

        }

    }

    @Override
    public postPhotoResponse postPhoto(String data, String imageType, String topnum, String store) {
        Env.info("人脸识别上传图片：imageType:"+imageType+"---topnum:"+topnum+"---store:"+store);
        Map<String, String> params = new HashMap<>();
        params.put("base64image",data);
        params.put("imageType",imageType);
        params.put("topnum",topnum);
        params.put("store",store);
        okhttp3.Call call = Env.getNetConnect().postComplexForm(Env.getAppContext().scanFaceUrl + "/face/verify",params);
        final postPhotoResponse response1 = new postPhotoResponse();

        try{
            call.enqueue(new Callback() {
                @Override
                public void onFailure(okhttp3.Call call, IOException e) {
                    response1.setErrMsg("识别超时，请检查网络后，再重新识别");
                    Env.info(response1.getErrMsg());
                    postPhotoResult.failure(response1);
                }

                @Override
                public void onResponse(okhttp3.Call call, Response response) {
                    String str = null;
                    try {
                        str = response.body().string();
                        Env.debug(str);

                        if(!str.isEmpty()){
                            JsonObject jsonObject = new JsonParser().parse(str).getAsJsonObject();
                            JsonElement errCode = jsonObject.get("errorCode");
                            if(errCode == null){
                                response1.errcode = jsonObject.get("errcode").getAsString();
                                response1.errmsg = jsonObject.get("errmsg").getAsString();
                                if(response1.errmsg.equals("ok")){
                                    JsonElement element = jsonObject.getAsJsonObject("content");
                                    JsonObject content = new JsonParser().parse(element.toString()).getAsJsonObject();
                                    JsonArray array = content.getAsJsonArray("result");
                                    JsonObject result = new JsonParser().parse(array.get(0).toString()).getAsJsonObject();
                                    JsonArray array1 = result.getAsJsonArray("person");
                                    JsonObject person = new JsonParser().parse(array1.get(0).toString()).getAsJsonObject();
                                    response1.idno = person.get("idno").getAsString();
                                    if(response1.idno.equals("")){
                                        response1.setErrMsg("识别通过，平台未记录该身份信息");
                                        Env.info(response1.getErrMsg());
                                        postPhotoResult.failure(response1);
                                    }else{
                                        response1.score = person.get("score").getAsString();

                                        if((int)Math.floor(Double.parseDouble(response1.score)) > Env.getAppContext().scanVerify){
                                            response1.facepath = person.get("facepath").getAsString();
                                            response1.identity = person.get("identity").getAsString();
                                            response1.personid = person.get("personid").getAsString();
                                            postPhotoResult.success(response1);
                                        }else{
                                            response1.setErrMsg("识别度太低");
                                            Env.info(response1.getErrMsg());
                                            postPhotoResult.failure(response1);
                                        }

                                    }

                                }else{
                                    response1.setErrMsg("识别不通过，未找到对应人脸信息");
                                    Env.info(response1.getErrMsg());
                                    postPhotoResult.failure(response1);

                                }
                            }else{
                                response1.setErrMsg( "识别失败，请联系管理员");
                                Env.info(response1.getErrMsg());
                                postPhotoResult.failure(response1);
                            }
                        }

                    } catch (IOException e) {
                        response1.setErrMsg("访问失败了");
                        postPhotoResult.failure(response1);
                        Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
                    }
                }
            });

        }catch (Exception e){
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));

        }

        return null;
    }

    @Override
    public getClassTypeResponse getClassType() {
        String url = Env.getTargetUrl().getClassTypeUrl;
        String flag = "获取班牌模式 :";

        String result = Env.getNetConnect().getConnect(flag,url);
        getClassTypeResponse response = new getClassTypeResponse();
        try {
            if (!result.isEmpty()) {
                if (!result.equals("error")) {
                    JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                    response.Value = jsonObject.get("Model").getAsString();
                    if (jsonObject.has("ChooseStartMechine") && jsonObject.has("ChooseShutdownMechine")) {
                        response.ChooseStartMechine = jsonObject.get("ChooseStartMechine").getAsString().isEmpty() ? "8:00" : jsonObject.get("ChooseStartMechine").getAsString();
                        response.ChooseShutdownMechine = jsonObject.get("ChooseShutdownMechine").getAsString().isEmpty() ? "18:00" : jsonObject.get("ChooseShutdownMechine").getAsString();
                    } else {
                        response.ChooseStartMechine = "8:00";
                        response.ChooseShutdownMechine = "18:00";
                    }
                    if (jsonObject.has("CardReadType") && jsonObject.has("DevicePath")) {
                        response.CardReadType = jsonObject.get("CardReadType").getAsString();
                        response.DevicePath = jsonObject.get("DevicePath").getAsString();
                    } else {
                        response.CardReadType = "";
                        response.DevicePath = "";
                    }
                    response.setSuccess(true);
                }
            } else {
                response.setErrMsg("网络错误，请检查网络");
                Env.info(response.getErrMsg());
            }
        }catch (Exception e){
            response.setErrMsg("访问失败了");
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));

        }finally {
            return response;
        }

    }

    @Override
    public getLeaveRecordResponse getLeaveRecord(int pageIndex, int pageSize) {
        String url = Env.getTargetUrl().getLeaveRecordUrl +  TargetUrl.pageIndex + pageIndex + TargetUrl.pageSize + pageSize;
        String flag = "获取请假记录 :";

        String result = Env.getNetConnect().getConnect(flag,url);
        getLeaveRecordResponse response = new getLeaveRecordResponse();
        try{
            if (!result.isEmpty()) {
                if (!result.equals("error")) {
                    JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                    response.PageCount = jsonObject.get("PageCount").getAsString();
                    response.PageIndex = jsonObject.get("PageIndex").getAsString();
                    response.PageSize = jsonObject.get("PageSize").getAsString();
                    JsonArray array = jsonObject.get("EntityList").getAsJsonArray();
                    for (JsonElement element : array) {
                        response.list.add((LeaveInfo) gson.fromJson(element, new TypeToken<LeaveInfo>() {
                        }.getType()));
                    }
                    response.setSuccess(true);
                }
            }else{
                response.setErrMsg("网络错误，请检查网络");
                Env.info(response.getErrMsg());
            }
        }catch (Exception e) {
            response.setErrMsg("访问失败了");
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
        }finally {
            return response;

        }
    }

    @Override
    public getLeaveTypeResponse getLeaveType(String id) {
        String url = Env.getTargetUrl().getLeaveTypeUrl+id;
        String flag = "获取请假类型 :";

        String result = Env.getNetConnect().getConnect(flag,url);
        getLeaveTypeResponse response = new getLeaveTypeResponse();
        try {
            if (!result.isEmpty()) {
                if (!result.equals("error")) {
                    JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();

                    if (!id.equals("1")) {
                        response.Id = jsonObject.get("Id").getAsString();
                        response.StudentId = jsonObject.get("StudentId").getAsString();
                        response.GradeClassName = jsonObject.get("GradeClassName").getAsString();
                        response.LeaveTypeText = jsonObject.get("LeaveTypeText").getAsString();
                        response.LeaveStateText = jsonObject.get("LeaveStateText").getAsString();
                        response.SemesterName = jsonObject.get("SemesterName").getAsString();
                    }
                    JsonArray array = jsonObject.get("LeaveTypeList").getAsJsonArray();
                    for (JsonElement element : array) {
                        response.list.add((LeaveTypeInfo) gson.fromJson(element, new TypeToken<LeaveTypeInfo>() {
                        }.getType()));
                    }
                    response.setSuccess(true);
                }
            } else {
                response.setErrMsg("网络错误，请检查网络");
                Env.info(response.getErrMsg());
            }
        }catch (Exception e) {
            response.setErrMsg("访问失败了");
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
        }finally {
            return response;

        }
    }

    @Override
    public applyLeaveResponse applyLeave(String type , String typeText , String startTime , String endTime, String reason) {
        String url = Env.getTargetUrl().applyLeaveUrl +"&LeaveType="+type+"&LeaveTypeText="
                + URLEncoder.encode(typeText)+"&LeaveStart="+startTime+"&LeaveEnd="+endTime+"&LeaveReason="+reason;
        String flag = "请假 :";

        String result = Env.getNetConnect().getConnect(flag,url);
        applyLeaveResponse response = new applyLeaveResponse();
        try{
            if (!result.isEmpty()) {
                if (!result.equals("error")) {
                    JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                    if ( jsonObject.get("res").getAsString().equals("success")) {
                       response.setSuccess(true);
                    }
                }
            }else
            {
                response.setErrMsg("网络错误，请检查网络");
                Env.info(response.getErrMsg());
            }
        }catch (Exception e) {
            response.setErrMsg("访问失败了");
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
        }finally {
            return response;

        }
    }

    @Override
    public WeiChatNewsResponse WeiChatNews(String Id, String typeText, String startTime, String endTime, String reason) {
        String url = Env.getTargetUrl().WeiXinToPerson ;
        Map<String, Object> params = new HashMap<>();
        params.put("sn",Id);
        params.put("type",typeText);
        params.put("reason",reason);
        params.put("time_start",startTime);
        params.put("time_stop",endTime);
        Call call = Env.getNetConnect().postNet(url,params);
        final WeiChatNewsResponse response = new WeiChatNewsResponse();
        try{
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    weiXinResult.failure(response);
                }

                @Override
                public void onResponse(Call call, Response response1) throws IOException {
                    try {
                        String result = response1.body().string();
                        if (!result.isEmpty()) {
                            if (!result.equals("error")) {
                                JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                                if (jsonObject.get("result").getAsString().equals("0")) {
                                    response.setSuccess(true);
                                }
                            }
                        } else {
                            response.setErrMsg("网络错误，请检查网络");
                            Env.info(response.getErrMsg());
                        }
                    }catch (Exception e) {
                       response.setErrMsg("访问失败了");
                        weiXinResult.failure(response);
                        Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
                    }
                }
            });
        }catch (Exception e){

            weiXinResult.failure(response);
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
        }

        return response;
    }

    public interface postPhotoResult{
        void success(postPhotoResponse response);
        void failure(postPhotoResponse response);
    }

    public postPhotoResult postPhotoResult = null;
    public void setPostPhoto(postPhotoResult result){
        this.postPhotoResult = result;
    }

    public interface postWeiXinResult{
        void success(WeiChatNewsResponse response);
        void failure(WeiChatNewsResponse response);
    }

    public postWeiXinResult weiXinResult = null;
    public void setWeiXinResult(postWeiXinResult result){
        this.weiXinResult = result;
    }
}
